/*

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#import "XBMCControlApplication.h"
#import <UIKit/UIPreferencesTable.h>
#import "MusicView.h"
#import "SectionMusicView.h"
#import "SectionVideosView.h"
#import "NowPlayingView.h"
#import "SettingsView.h"
#import "MediaQueueView.h"
#import "IconManager.h"

@implementation XBMCControlApplication

- (void) applicationDidFinishLaunching: (id) unused
{
    NSLog(@"XBMCControlApplication: applicationDidFinishLaunching");
    struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
    rect.origin.x = rect.origin.y = 0.0f;

    m_window = [[UIWindow alloc] initWithContentRect: rect];
	m_transitionView = [[UITransitionView alloc] initWithFrame:rect];	
	m_mainMenuView = [[UIView alloc] initWithFrame: rect];
	
	// make music view
	m_musicView = [[SectionMusicView alloc] initWithApplication: self
		withAppID: @"xbmc"
		withFrame: rect];
	// make videos view
	m_videosView = [[SectionVideosView alloc] initWithApplication: self
		withAppID: @"xbmc"
		withFrame: rect];
    // make settings view
	m_settingsView = [[SettingsView alloc] initWithApplication: self
		withAppID: @"xbmc"
		withFrame: rect];
	// make media queue view
	m_mediaQueueView = [[MediaQueueView alloc] initWithApplication: self
		withAppID: @"xbmc"
		withFrame: rect];
	// make now playing view
	m_nowPlayingView = [[NowPlayingView alloc] initWithApplication: self
		withAppID: @"xbmc"
		withFrame: rect];
		
    [m_window orderFront: self];
    [m_window makeKey: self];
	#ifdef __OBJC2__
	// this is deprecated and seemingly not needed
	//[m_window _setHidden: NO];
	#else
    [m_window _setHidden: NO];
	#endif
	
	
	[m_transitionView addSubview: m_mainMenuView];
	[m_transitionView addSubview: m_musicView];
	[m_transitionView addSubview: m_videosView];
	[m_transitionView addSubview: m_settingsView];
	[m_transitionView addSubview: m_mediaQueueView];
	[m_transitionView addSubview: m_nowPlayingView];
	
	// nav bar
    m_navBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, rect.size.width, 48.0f)] autorelease];
    [m_navBar showLeftButton:@"About" withStyle:0 rightButton:@"Settings" withStyle:3];
    [m_navBar setBarStyle:0];
    [m_navBar setDelegate:self];
    [m_mainMenuView addSubview: m_navBar];

    // title
    UINavigationItem *title = [[[UINavigationItem alloc] initWithTitle:@"XBMC Control"] autorelease];
    [m_navBar pushNavigationItem:title];
	
    // section table
	m_sectionList = [[UISectionList alloc] initWithFrame: CGRectMake(0.0f, 48.0f, rect.size.width, rect.size.height - 48.0f) showSectionIndex:NO];
	[m_sectionList setDataSource:self];
	[m_sectionList setSectionListStyle:0];
	[m_mainMenuView addSubview: m_sectionList];
	
	
	m_table = [m_sectionList table];
	[m_table setShouldHideHeaderInShortLists:NO];
	
	UITableColumn * packageColumn = [[UITableColumn alloc] initWithTitle:@"Title" identifier:@"title" width:320.0f];
	[m_table addTableColumn:packageColumn];
	[m_table setSeparatorStyle:1];
	[m_table setRowHeight:48.0];
	[m_table setDelegate:self];
	[m_table setAllowsReordering:YES];  
	[m_table setAllowsScrollIndicators:YES];
    
    [self createMenuCells];
    
    [m_window setContentView: m_transitionView];
	[m_transitionView transition:0 toView:m_mainMenuView];
	
	[m_sectionList reloadData];
}

- (void) showProgress: (BOOL)show
{
	[self setStatusBarShowsProgress: show];
}


- (void)createMenuCells
{
    UIImageView *iconView;
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
    
	// create the cells
	m_CellArray = [[NSMutableArray alloc] init];
	
	// media
	m_musicCell = [[UIImageAndTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_musicCell setTitle:@"Music"];
	[m_musicCell setShowDisclosure:YES];
	iconView = [m_musicCell iconImageView];
	[iconView setImage: [[IconManager sharedInstance] getMusicIcon]];
	[iconView setFrame: CGRectMake(0.0f, 0.0f, 29.0f, 29.0f)];
	[m_CellArray addObject:m_musicCell];
	
	m_videosCell = [[UIImageAndTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_videosCell setTitle:@"Videos"];
	[m_videosCell setShowDisclosure:YES];
	iconView = [m_videosCell iconImageView];
	[iconView setImage: [[IconManager sharedInstance] getVideosIcon]];
	[iconView setFrame: CGRectMake(0.0f, 0.0f, 29.0f, 29.0f)];
	[m_CellArray addObject:m_videosCell];
	
	m_picturesCell = [[UIImageAndTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_picturesCell setTitle:@"Pictures"];
	[m_picturesCell setShowDisclosure:YES];
	iconView = [m_picturesCell iconImageView];
	[iconView setImage: [[IconManager sharedInstance] getPicturesIcon]];
	[iconView setFrame: CGRectMake(0.0f, 0.0f, 29.0f, 29.0f)];
	[m_CellArray addObject:m_picturesCell];
	
	m_filesCell = [[UIImageAndTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_filesCell setTitle:@"Files"];
	[m_filesCell setShowDisclosure:YES];
	iconView = [m_filesCell iconImageView];
	[iconView setImage: [[IconManager sharedInstance] getFilesIcon]];
	[iconView setFrame: CGRectMake(0.0f, 0.0f, 29.0f, 29.0f)];
	[m_CellArray addObject:m_filesCell];
	
	// playlists
	m_musicPlaylistsCell = [[UIImageAndTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_musicPlaylistsCell setTitle:@"Music Playlists"];
	[m_musicPlaylistsCell setShowDisclosure:YES];
	[m_CellArray addObject:m_musicPlaylistsCell];
	
	m_videoPlaylistsCell = [[UIImageAndTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_videoPlaylistsCell setTitle:@"Video Playlists"];
	[m_videoPlaylistsCell setShowDisclosure:YES];
	[m_CellArray addObject:m_videoPlaylistsCell];
	
	// status
	m_nowPlayingCell = [[UIImageAndTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_nowPlayingCell setTitle:@"Now Playing"];
	[m_nowPlayingCell setShowDisclosure:YES];
	[m_CellArray addObject:m_nowPlayingCell];
	
	m_queuedItemsCell = [[UIImageAndTextTableCell alloc] initWithFrame:CGRectMake(0.0f, 48.0f, rect.size.width, 48.0f)];
    [m_queuedItemsCell setTitle:@"Media Queue"];
	[m_queuedItemsCell setShowDisclosure:YES];
	[m_CellArray addObject:m_queuedItemsCell];
}

- (int)numberOfSectionsInSectionList:(UISectionList *)aSectionList {
    NSLog(@"numberOfSectionsInSectionList  (3)");
	return 3;
}

     
- (NSString *)sectionList:(UISectionList *)aSectionList titleForSection:(int)row {	
	switch(row)
	{
	   case 0:
		return @"Media";
	   case 1:
		return @"Playlists";
		case 2:
		return @"Status";
	}
}       

- (int)sectionList:(UISectionList *)aSectionList rowForSection:(int)row {
	// this is what determines the placement of the header
	switch(row)
	{
	   case 0:
		return 0;
	   case 1:
		return 4;
	   case 2:
		return 6;
	}
}

- (int) numberOfRowsInTable: (UISectionTable*)table
{
	return [m_CellArray count];
}

- (UITableCell*) table: (UISectionTable*)table cellForRow: (int)row column: (int)col
{
    NSLog(@"cellForRow: %d  Column: %d", row, col);
	return [m_CellArray objectAtIndex: row];
}


- (void)navigationBar:(id)navbar buttonClicked:(int)button
{
    switch (button)
    {
        case 0:
            [self switchToSettingsView];
            break;
        case 1:
            // do about
            // TODO
            break;
    }
}

- (void)tableRowSelected:(NSNotification *)notification 
{
  int i = [m_table selectedRow];
  NSLog(@"tableRowSelected! Row %d!", i);

  if (m_musicCell == [m_CellArray objectAtIndex: i])
  {
	    [self switchToMusicView];
  }
  else if (m_videosCell == [m_CellArray objectAtIndex: i])
  {
	    [self switchToVideosView];
  }
  else if (m_nowPlayingCell == [m_CellArray objectAtIndex: i])
  {
		[self switchToNowPlayingView];
  }
  else if (m_queuedItemsCell == [m_CellArray objectAtIndex: i])
  {
		[self switchToMediaQueueView];
  }
  else
  {
        NSArray* buttons = [NSArray arrayWithObjects:@"Close",nil];
        UIAlertSheet* popup = [[UIAlertSheet alloc] initWithTitle:@"Not Implemented" buttons:buttons defaultButtonIndex:0 delegate:self context:nil];
		[popup setBodyText: @"Not Implemented!"];
		[popup popupAlertAnimated: TRUE];
  }
  
  NSLog(@"end tableRowSelected! Row %d!", i);
  
  
}

- (void)alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button 
{
    int i=0;
   for (i=0; i<[m_CellArray count]; ++i)
   { 
      [[m_table cellAtRow:i column:0] setSelected:NO];
   }
	[sheet dismissAnimated: TRUE];
}

- (void)switchToMusicView
{
  NSLog(@"switchToMusicView");
  [m_transitionView transition:1 toView:m_musicView];
  m_mainMenuTransitionType = 2;
  [m_musicView activateView];
  NSLog(@"switchToMusicView done");
}

- (void)switchToVideosView
{
  NSLog(@"switchToVideosView");
  [m_transitionView transition:1 toView:m_videosView];
  m_mainMenuTransitionType = 2;
  [m_videosView activateView];
  NSLog(@"switchToVideosView done");
}

- (void)switchToNowPlayingView
{
  NSLog(@"switchToNowPlayingView");
  [m_transitionView transition:1 toView:m_nowPlayingView];
  m_mainMenuTransitionType = 2;
  [m_nowPlayingView activateView];
  NSLog(@"switchToNowPlayingView done");
}

- (void)switchToMediaQueueView
{
  NSLog(@"switchToMediaQueueView");
  [m_transitionView transition:3 toView:m_mediaQueueView];
  m_mainMenuTransitionType = 2;
  [m_mediaQueueView activateView];
  NSLog(@"switchToMediaQueueView done");
}

- (void)switchToSettingsView
{
  NSLog(@"switchToSettingsView");
  [m_transitionView transition:3 toView:m_settingsView];
  m_mainMenuTransitionType = 7;
  NSLog(@"switchToSettingsView done");
}

- (void)exit
{
	[self terminateWithSuccess];
}

- (void) transitionToMainMenu
{
   int i=0;
   for (i=0; i<[m_CellArray count]; ++i)
   { 
      [[m_table cellAtRow:i column:0] setSelected:NO];
   }
   [m_musicView deactivateView]; // check if it's active!!!!!
   [m_nowPlayingView deactivateView]; // check if it's active!!!!!
   [m_mediaQueueView deactivateView]; // check if it's active!!!!!
   
   [m_transitionView transition:m_mainMenuTransitionType toView:m_mainMenuView];
}




@end

